﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYRevitCode.RevitDimensionUtility;
using YJKStairs.CommonUnit;
using YJKStairs.DataInfos.DimesionData;

namespace YJKStairs.Manage
{
	public class StairsClearHeightDimensionCreator
	{
		public static bool CreateClearHeightDimension(ExternalCommandData commandData)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			ElementId id = document.ActiveView.Id;
			Transaction transaction = new Transaction(document, "createDimType");
			transaction.Start();
			new DimesionTypeBuilder(document);
			DimensionType dimensionType = RevitDimensionUtiltyMethod.GetDimensionType(document, DimensionTypeEnum.ScaleDimension, null);
			if (dimensionType == null)
			{
				MessageBox.Show("错误： 楼梯标注类型创建失败！", "盈建科提示");
				transaction.RollBack();
				return false;
			}
			transaction.Commit();
			List<DimesionDataManager> list = new List<DimesionDataManager>();
			Transaction transaction2 = new Transaction(document, "dimension");
			transaction2.Start();
			StairsClearHeightMgr stairsClearHeightMgr = new StairsClearHeightMgr(document, id);
			stairsClearHeightMgr.InitData(StairsClearHeightMgr.InitDataType.INIT_DMSCLEARHEIGHT);
			try
			{
				ElementId id2 = dimensionType.Id;
				stairsClearHeightMgr.DrawStairsDimesion(StairsClearHeightMgr.DimObjType.DOT_RUN, ref list, id2);
				stairsClearHeightMgr.DrawStairsDimesion(StairsClearHeightMgr.DimObjType.DOT_LAND, ref list, id2);
				transaction2.RollBack();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
				transaction2.RollBack();
			}
			if (list.Count > 0)
			{
				Transaction transaction3 = new Transaction(document, "createdim");
				transaction3.Start();
				try
				{
					foreach (DimesionDataManager dimesionDataManager in list)
					{
						dimesionDataManager.CreateDimsion(document);
					}
					transaction3.Commit();
				}
				catch
				{
					transaction3.RollBack();
				}
			}
			return true;
		}
	}
}
